#include "pub_defs.h"
#include "calc_xv_baseline.h"
#include <iostream>

int main() {
    int tmax = TMAX;
    int n = N;
    double m = M;
    double dt = DT;
    TD* x = new TD[n];
    TD* v = new TD[n];

    // 将输入数据读取到x和v数组中
    if (read_infile(x, v, n)) {
        std::cerr << "Error: please execute the gen_data first!" << std::endl;
        delete[] x;
        delete[] v;
        return 1;
    }

    double begin_time = wall_time();
    calc_xv_baseline(x, v, n, tmax, m, dt);
    double last_time = wall_time()-begin_time;

    std::cout << "Time elapsed: " << last_time << " seconds." << std::endl;
    TD* xout = new TD[n];
    TD* vout = new TD[n];
    // 将正确的结构读取到xout和vout数组中
    if (read_outfile(xout, vout, n)) {
        std::cerr << "Error: cannot open output file, please execute the gen_data first!" << std::endl;
        delete[] x;
        delete[] v;
        delete[] xout;
        delete[] vout;
        return 1;
    }


    // 将计算结果和读取结果进行比对。
    bool xright = compare_array<TD>(x, xout, n);
    bool vright = compare_array<TD>(v, vout, n);

    if (!(xright && vright)) {
        if (!xright){
            std::cerr << "Error: x array is not correct!" << std::endl;
        }
        if (!vright){
            std::cerr << "Error: v array is not correct!" << std::endl;
        }
        delete[] x;
        delete[] v;
        delete[] xout;
        delete[] vout;
        return 1;
    }

    std::cout << "Output is correct!" << std::endl;
        
    delete[] x;
    delete[] v;
    delete[] xout;
    delete[] vout;
    return 0;
}